/*
 * @lc app=leetcode.cn id=202 lang=javascript
 *
 * [202] 快乐数
 */

// @lc code=start
/**
 * @param {number} n
 * @return {boolean}
 */
var isHappy = function(n) {
  const getList = (num) =>
    num
      .toString()
      .split("")
      .map((item) => parseInt(item));
  const getSum = (list) => list.reduce((sum, cur) => sum + cur * cur, 0);

  const map = new Map();
  while(n !== 1 && !map.has(n)) {
    map.set(n, 1);
    n = getSum(getList(n));
  }

  return n === 1;
};
// @lc code=end

